package com.gdssoft.pdaweb.service.ws.impl;

import javax.jws.WebService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.CrmWorkOrderDao;
import com.gdssoft.pdaweb.model.CrmWorkOrder;
import com.gdssoft.pdaweb.service.ws.CrmWorkOrderService;

@WebService(endpointInterface = "com.gdssoft.pdaweb.service.ws.CrmWorkOrderService", 
		serviceName="CrmWorkOrderService", targetNamespace="http://gdssoft.foxconn.com/")
public class CrmWorkOrderServiceImpl implements CrmWorkOrderService {
	
	private CrmWorkOrderDao dao;
	private transient final Log logger = LogFactory.getLog(getClass());
	
	public CrmWorkOrderServiceImpl(CrmWorkOrderDao dao) {
		this.dao = dao;
	}
	
	public boolean push(String activityUID, String houseNumber,
			String houseLocation, String contactPerson, String contactPhoneNo,
			String dutyPerson, String workDescription, String workPriority,
			String workStatus, String plannedCompletion, String comment,
			String srno, String createPerson) {
		
		logger.info("CRM客服工单推送，参数：" + activityUID + " | " + houseNumber
				 + " | " + houseLocation + " | " + contactPerson + " | " + contactPhoneNo
				 + " | " + dutyPerson + " | " + workDescription + " | " + workPriority
				 + " | " + workStatus + " | " + plannedCompletion + " | " + comment
				 + " | " + srno + " | " + createPerson);
		
		CrmWorkOrder cwo = new CrmWorkOrder();
		cwo.setActivityUID(activityUID);
		cwo.setHouseNumber(houseNumber);
		cwo.setHouseLocation(houseLocation);
		cwo.setContactPerson(contactPerson);
		cwo.setContactPhoneNo(contactPhoneNo);
		cwo.setDutyPerson(dutyPerson);
		cwo.setWorkDescription(workDescription);
		cwo.setWorkPriority(convertWorkPriority(workPriority));
		cwo.setWorkStatus(convertWorkStatus(workStatus));
		cwo.setPlannedCompletion(DateUtil.dateParse(plannedCompletion, "MM/dd/yyyy hh:mm:ss"));
		cwo.setComment(comment);
		cwo.setSrno(srno);
		cwo.setContactPerson(createPerson);
		cwo.setCreatePerson("CrmService");
		
		Long id = dao.save(cwo);
		if (id > 0) {
			logger.info("CRM客服工单写入成功");
			return true;
		}
		else {
			logger.warn("CRM客服工单写入失败");
			return false;
		}
		
	}
	
	private String convertWorkStatus(String srcWorkStatus) {
		if (srcWorkStatus.equals("未处理"))
			return "1";
		else {
			logger.warn("CRM推送的客服工单状态：\"" + srcWorkStatus + "\"，无法识别~~");
			return "99";
		}
	}
	
	private String convertWorkPriority(String srcWorkPriority) {
		int index = srcWorkPriority.indexOf("-");
		if (index>0) {
			return srcWorkPriority.substring(index+1, srcWorkPriority.length());
		} else {
			return srcWorkPriority;
		}
	}
	/*
	public static void main(String[] args) {
		CrmWorkOrderServiceImpl c = new CrmWorkOrderServiceImpl(null);
		System.out.println(c.convertWorkStatus("未处理"));
		System.out.println(c.convertWorkStatus("来处理"));
		System.out.println(c.convertWorkPriority("1-尽快"));
	}*/
}
